import 'package:cached_network_image/cached_network_image.dart';
import 'package:flutter/material.dart';
import 'package:lottie/lottie.dart';



class CachedImage extends StatefulWidget {
  final String imageUrl;
  final double? width;
  final double? height;
  final BoxFit? fit;

  const CachedImage(
      {super.key,
      required this.imageUrl,
      this.width,
      this.height,
      this.fit = BoxFit.cover});

  @override
  State<CachedImage> createState() => _CachedImageState();
}

class _CachedImageState extends State<CachedImage> {
  String url = "";


  @override
  void initState() {
    super.initState();
  }

  @override
  void dispose() {
    super.dispose();
  }

  @override
  Widget build(BuildContext context) {
    url = widget.imageUrl;

    return CachedNetworkImage(
      imageUrl: url,
      width: widget.width,
      height: widget.height,
      fit: widget.fit,
      fadeOutDuration: Duration.zero,
      fadeInDuration: Duration.zero,
      placeholder: (context, url) => AspectRatio(
        aspectRatio: 1,
        child: Container(
          height: double.infinity,
          width: double.infinity,
          color: const Color(0xFFF8F8F8),
          child: Center(
            child:
                Lottie.asset("assets/anim/image_loading/data.json", height: 38),
          ),
        ),
      ),
      errorWidget: (context, url, error) => AspectRatio(
        aspectRatio: 1,
        child: Container(
          height: double.infinity,
          width: double.infinity,
          color: const Color(0xFFF8F8F8),
          child: Center(
            child: GestureDetector(
                onTap: () {
                  setState(() {
                    url = widget.imageUrl;
                  });
                },
                child: Image.asset("assets/images/ic_welcome_logo.png",
                    width: 35)),
          ),
        ),
      ),
      errorListener: (e) {

      },
    );
  }
}
